//
//  CHSViewController.m
//  CAIDBManager
//
//  Created by 海参 on 10/26/2021.
//  Copyright (c) 2021 海参. All rights reserved.
//

#import "CHSViewController.h"
#import <CAIStorageManager/CAIStorageManager.h>
#import <objc/runtime.h>
#import <YYModel/YYModel.h>
#import "NSObject+ZXJSONCategory.h"

@interface Person : NSObject<NSSecureCoding>

@property (nonatomic, strong) NSNumber *age;

@property (nonatomic, copy) NSString *name;

@end

@implementation Person

- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    if (self) {
        unsigned int outCount;
        Ivar * ivars = class_copyIvarList([self class], &outCount);
        for (int i = 0; i < outCount; i ++) {
            Ivar ivar = ivars[i];
            NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
            [self setValue:[aDecoder decodeObjectForKey:key] forKey:key];
        }
        free(ivars);
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    unsigned int outCount;
    Ivar * ivars = class_copyIvarList([self class], &outCount);
    for (int i = 0; i < outCount; i ++) {
        Ivar ivar = ivars[i];
        NSString * key = [NSString stringWithUTF8String:ivar_getName(ivar)];
        [aCoder encodeObject:[self valueForKey:key] forKey:key];
    }
    free(ivars);
}

+ (BOOL)supportsSecureCoding{
    return YES;
}

@end


@interface CHSViewController ()

@end

@implementation CHSViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
   
    [self testCache];
    [self testDB];

}

- (void)testCache{
 
    //plist-kv存储
    [[CAIStorageManager sharedInstance]setObject:@"1111" forKey:@"person"];
    NSDate *date = [NSDate date];
    [[CAIStorageManager sharedInstance]setObject:date forKey:@"person"];


    //archive对象存储-kv
    Person *person = [[Person alloc] init];
    person.name = @"123";
    person.age = @(19);
    [[CAIStorageManager sharedInstance]removeObjectForKey:@"person1"];
    [[CAIStorageManager sharedInstance]archiveObject:person forKey:@"person1"];
    
//    NSString *arr = @"[\"key\",\"key2\"]";
    NSArray *arr = @[@"/key9",@"key2"];
    id objc = [NSObject zx_getJSONSerializationStringFromJSONObject:arr];
    
    //统一取对象
    id obj = [[CAIStorageManager sharedInstance]objectForKey:@"person"];
    
    //统一方式根据key删除对象
    [[CAIStorageManager sharedInstance]removeObjectForKey:@"person"];

    
    //plist默认加密存储
    [[CAIStorageManager sharedInstance]setEncodeCryptObject:@"012345" forKey:@"person2" error:nil];
    
    //加密存储中打印错误信息
    NSError *error = nil;
    [[CAIStorageManager sharedInstance]setEncodeCryptObject:@"012345" forKey:@"person3" error:&error];
    NSLog(@"%@",error);

    //archive默认加密存储
    Person *person4 = [[Person alloc] init];
    person4.name = @"123";
    person4.age = @(19);
    [[CAIStorageManager sharedInstance]archiveEncodeCryptObject:person4 forKey:@"person4" error:nil];

    //统一方式解密获取对象--模型数据
    id  data2 = [[CAIStorageManager sharedInstance]objectDecodeCryptForKey:@"person4" expectedClass:[Person class] error:nil];
    
    //统一方式删除对象
    [[CAIStorageManager sharedInstance]removeObjectForKey:@"person"];
    
    
    //archive对象存储-kv
    Person *person5 = [[Person alloc] init];
    person5.name = @"123";
    person5.age = @(19);
    NSString *str = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    CAIStorageManager *manager =  [[CAIStorageManager alloc]initWithName:nil rootPath:str];
    [manager archiveObject:person forKey:@"person5"];
}




- (void)testDB{

    Person *person = [[Person alloc] init];
    person.name = @"123";
    person.age = @(19);

    //数据库设置model数据
    [[CAIStorageManager sharedInstance]setTable:@"User2" model:person error:nil];
    
    //数据库设置model数据-加密方式
    [[CAIStorageManager sharedInstance]setEncodeWithTable:@"User2" model:person error:nil];
    
    //数据库取model数据
    id data = [[CAIStorageManager sharedInstance]fetchDecodeWithTable:@"User2" modelClass:[Person class]];
}





/// 数据库额外提供的原子方式
- (void)testDB2{
    
    CAIFMDB *db = [CAIFMDB sharedInstance];
    BOOL drop = [db dropTable:@"User"];
    
    NSError *error = nil;
    BOOL creatFlag = [db createTable:@"User" dic:@{@"name":@"TEXT",@"age":@"INTEGER"} error:&error];

    BOOL insetFlag1 =[db insertTable:@"User" dic:@{@"name":@"海参",@"age":@20,@"address":@"杭州"}];
    BOOL insetFlag2 =[db insertTable:@"User" dic:@{@"name":@"荔枝",@"age":@19,@"address":@"安徽"}];

    NSArray *arr1 = [db fetchTable:@"User" dic:@{@"name":@"TEXT",@"age":@"INTEGER"} whereFormat:nil];

    BOOL deleteFlag1 = [db deleteTable:@"User" whereFormat:@"where name = '荔枝'"];

    BOOL udpateFlag1 = [db updateTable:@"User" dic:@{@"name":@"海参",@"age":@30} whereFormat:@"where name = '海参'"];

    NSArray *arr2 = [db fetchTable:@"User" dic:@{@"name":@"TEXT",@"age":@"INTEGER"} whereFormat:nil];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
